1. Technical Field
The present invention is directed to an atomic memory migration method and apparatus. More specifically, the present invention is directed to a method of migrating memory that is the target of a Direct Memory Access (DMA), i.e. memory-to-memory, operation.
2. Description of Related Art
The migration of memory is a key building block used to support such operations as dynamic removal of memory or dynamic accumulation of physically contiguous page frames. For memory pages that are only referenced by central processing units (CPUs), there are straightforward methods for ensuring atomic access to the memory page, such as removing all translations to the page and spinning in fault handlers if the page is undergoing migration. However, pages that are also the targets of Direct Memory Access (DMA) operations by external input/output (I/O) agents are far more difficult to manage.
The difficulty in migrating memory that is the target of DMA operations is that the DMA operations are not performed by the CPUs and thus, the CPU cannot simply be placed in a fault handler spin state during the migration period. It is necessary to ensure that all possible outstanding DMA operations by I/O agents on the page that is subject to migration, are suspended during the period of the migration. This is difficult because I/O agents in the system represent autonomous processing units that can asynchronously read and write system memory. The operating system is physically executed on the system""s central processing units (CPUs) and can synchronously control the access to system memory from CPUs. The operating system has no direct control over the external I/O agents.
Thus, it would be beneficial to have a method and apparatus for migrating memory that is the target of DMA operations.
The present invention provides an atomic memory migration apparatus and method. With the present invention, all active DMA mappings to a given physical page of memory are identified and future mappings/unmappings to the physical page are serialized with migrations of current mappings. The identified DMA mappings are then disabled at the bus level and the physical page is migrated to a new memory page. All existing DMA mappings are also migrated to the new page. After migration is completed, the DMA mappings, which have now been migrated to the new page, are reenabled at the bus level.
With the present invention DMA mappers, i.e. DMA support layers such as drivers and management software for bus bridges or the like that are responsible for handling DMA operations, register with the operating system kernel as a DMA mapper. The DMA mappers include an event handler that is called for various page migration state transitions and adapts the DMA mappers to conform to the page migration state machine of the present invention.
The page migration state machine has six primary states: normal operation, memory migration pending, page query, page migrate start, page migrate end, and memory migration complete. In normal operation, the DMA mappers have their own registered set of devices/drivers for which they are managing DMA operations/mappings. In this mode, no explicit serialization is performed by the DMA mapper as it executes only in the context of the device driver which calls it, which is already serialized with respect to this device and DMA pool. The DMA mapper keeps a record of all active DMA mappings on a per-device granularity.
In response to a request to migrate a portion of memory identified by a range of addresses, the state machine transitions to the memory migration pending state. In the memory migration pending state, notification that there will be memory migration requests is sent from the operating system kernel to the DMA mappers. This notification informs the DMA mappers to incur the cost of building and maintaining mapping information data in order to track all DMA mappings to the specific address range. In addition, the notification causes the DMA mapper to incur locking and serialization overhead that is not required during the normal operation state.
In response to receiving the notification that memory migration requests will be sent, the DMA mapper sets a flag indicating that locking is required in all mapping paths and issues a synchronous interprocessor interrupt to all CPUs in the system. The interrupt is sent at an interrupt level less favored than the interrupt level of DMA operations. Thus, once all CPUs in the system have acknowledged the interrupt, the DMA mapper knows that all DMA critical operations have exited and new critical sections will see the flag requiring serialization.
The DMA mapper then serializes with the mapping and unmapping paths and scats the outstanding DMA mappings for mappings within the range of the migration target address range. The DMA mapper then establishes data structures to manage each of these DMA mappings that fall within the target address range.
The state machine transitions to the page query state in response to a query from the operating system, which is driving the page migration, as to whether the DMA mapper has a particular page mapped for DMA operations. In response to receiving this query, the DMA mapper serializes with new mappings/unmappings and looks to see if the specified page is DMA mapped. If the page is DMA mapped by the DMA mapper, a predetermined time period may be allowed to expire before the transition to the next state, page migrate start, is performed in order to allow for transient DMA mappings to expire.
Once the operating system kernel has performed all the necessary queries to the registered DMA mappers, the operating system kernel sends a notification to the DMA mappers that a specified page is now in a migration state and the state machines of the event handlers of the DMA mappers transition to the page migrate start state. In response to receiving this notification, the DMA mapper sets internal state so that any unmap attempts to the in-migrate page are deferred. The DMA mapper then accumulates all remaining active DMA mappings to the page and reports them to the operation system kernel. The operating system kernel, after accumulating all the mappings for the page from all DMA mappers in the system, utilizes a platform specific facility to disable DMA arbitration for each DMA mapping found, migrate the old page to the new page, update each DMA mapping with the new page, and then re-enable DMA arbitration.
Once the page migration is completed, the operating system kernel notifies the DMA mappers of the end of the page migration, whether or not the page migration was completed successfully or not. Receipt of this notification causes the state machine of the event handlers of the DMA mappers to transition to the page migrate end state. The DMA mapper performs any deferred unmaps that might have been initiated when the page was in migrate state. The DMA mapper also updates internal data structures to reflect the DMA mappings that now map to the new memory page. The previous query-migrate start-migrate end states are repeated for each page in the range that was communicated in the migrate pending state, prior to the migrate complete state.
The operating system kernel then notifies the DMA mappers that there will be no more memory migration requests and the DMA mappers transition to the memory migration complete state. The DMA mapper relaxes the serialization and locking that was turned on during the memory migrate pending state and frees any additional data structures that were allocated in order to track DMA mappings to the specified memory address range. The completion of this state is a return to the normal operation state.
Thus, the present invention provides a mechanism for atomically migrating a memory page with active DMA mappings. Furthermore, the present invention provides a mechanism for atomically migrating a memory page with concurrent DMA activity.
These and other features and advantages of the present invention will be described in, or will become apparent to those of ordinary skill in the art in view of, the following detailed description of the preferred embodiments.